package com.example.myapplication.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tools.SocketTools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Sip2Activity extends Activity {

    SocketTools socketTools;

    String sip2_ip = "49.234.128.249";
    int sip2_port = 4700;

    String testSN = "sdef";
    String testAO = "50000";
    String testBarcode = "20171104";
    String testReaderNum = "0000010847";
//    String testAO = "54540";
//    String testBarcode = "20171104";
//    String testReaderNum = "0000010847";

    ImageView iv;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sip2);
        iv = findViewById(R.id.iv_touxiang);
        context = this;
        socketTools = new SocketTools("49.234.128.249", 4700, new SocketTools.ReadMessage() {
            @Override
            public void message(String msg) {
                Map<String,String> map = getMapInfo(msg);
                if(map == null){
                    Log.e("==========>>", "map is null");
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(map.containsKey("IM")){
                                String im = map.get("IM");
                                Glide.with(context)
                                        .load(im)
                                        .placeholder(R.color.transparent)//加载未获取图片之前的空白图片
                                        .error(R.color.transparent)//错误图片
                                        .into(iv);
                            }else{
                                Glide.with(context)
                                        .load(R.drawable.temp_60)
                                        .placeholder(R.color.transparent)//加载未获取图片之前的空白图片
                                        .error(R.color.transparent)//错误图片
                                        .into(iv);
                            }
                        }
                    });
                    if(map.containsKey("CM")){
                        String cm = map.get("CM");
                        if(cm.length() == 15){
                            try {
                                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd HHmmss");
                                Date d1 = sdf1.parse(cm);
                                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                map.put("CJ",sdf2.format(d1));
                            }catch (Exception e){
                                map.put("CJ",cm);
                            }
                        }else{
                            map.put("CJ",cm);
                        }
                    }
                    if(map.containsKey("AH")){
                        String ah = map.get("AH");
                        if(ah.length() == 15){
                            try {
                                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd HHmmss");
                                Date d1 = sdf1.parse(ah);
                                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                map.put("AH",sdf2.format(d1));
                            }catch (Exception e){
                            }
                        }
                    }

                    if(map.containsKey("BG")){
                        map.put("CA",map.get("BG"));
                    }
                    Log.e("TAG", "message: ");
                }
//                Log.e("=====>>", "message: " + msg);
            }

            @Override
            public void error(String errorInfo) {
                Log.e("=====>>", "error: " + errorInfo);
            }
        });
        new Thread(){
            @Override
            public void run() {
                socketTools.createSocket("");
            }
        }.start();
        Glide.with(context)
                .load(R.drawable.pic_item_task)
                .placeholder(R.color.transparent)//加载未获取图片之前的空白图片
                .error(R.color.transparent)//错误图片
                .into(iv);
    }

    public void onClickSip2(View v){
        switch (v.getId()){
            case R.id.button63://读者查询
                socketTools.send("6300120131111   1659201234567890SN" + testSN + "|AO" + testAO + "|AA" + testReaderNum + "|AD123456|AY1AZF4A6\r");
                break;
            case R.id.button17://图书查询
                socketTools.send("1720080828   105715SN" + testSN + "|AO" + testAO + "|AB" + testBarcode + "|AY1AZF7E3|\r");
                break;
            case R.id.button11://借书
                socketTools.send("11YN19960212   10051419960212   100514SN" + testSN + "|AO" + testAO + "|AA" + testReaderNum + "|AB" + testBarcode + "|AC|AY3AZEDB7\r");
                break;
            case R.id.button09://还书
                socketTools.send("09N20080831   155504SN" + testSN + "|AP|AO" + testAO + "|AB" + testBarcode + "|AC|BIN|AY1AZF0CC\r");
                break;
            case R.id.button29://续借
                socketTools.send("29XJ19960212    10051419960212   100514SN" + testSN + "|AO" + testAO + "|AA" + testReaderNum + "|AB" + testBarcode + "|AC|XJ3AZEDB7\r");
                break;
            case R.id.button_heard:
//                socketTools.send("test\r");
                socketTools.send("6300120131111   1659201234567890SN" + testSN + "|AO" + testAO + "|AAaa123|AD123456|AY1AZF4A6\r");
                break;
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public Map<String,String> getMapInfo(String str){
        Map<String,String> paramsMap = null;
        if(str.contains("AO")){
            str = str.substring(str.indexOf("AO"));
            String[] arr = str.split("\\|");
            paramsMap = new HashMap<>();
            for(String temp : arr){
                if(temp.length() == 0){
                    continue;
                }
                String keyword = temp.substring(0,2);
                String content = temp.substring(2);
//                paramsMap.put(Sip2NameChange.change(keyword),content);
                paramsMap.put(keyword,content);
            }
        }else if(str.contains("\\|") || str.contains("|")){
            int index = str.indexOf("\\|");
            if(index == -1){
                index = str.indexOf("|");
            }
            if(index == -1){
                return null;
            }
            str = str.substring(index);
            String[] arr = str.split("\\|");
            paramsMap = new HashMap<>();
            for(String temp : arr){
                if(temp.length() == 0){
                    continue;
                }
                String keyword = temp.substring(0,2);
                String content = temp.substring(2);
//                paramsMap.put(Sip2NameChange.change(keyword),content);
                paramsMap.put(keyword,content);
            }
        }
        return paramsMap;
    }
}